package com.aaa.javaweb.servlet;

import com.aaa.javaweb.dao.impl.StaffDaoimpl;
import com.aaa.javaweb.entity.Staff;
import com.aaa.javaweb.until.TransBean;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet("/staff")
public class StaffController extends HttpServlet {
    StaffDaoimpl staffDaoimpl = new StaffDaoimpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=UTF-8");
        String method = req.getParameter("method");
        System.out.println(method);
        if (method!=null){

            switch (method){
                case "add":
                    addstaff(req,resp);
                    break;
                case "juesexlk":
                    sel(req,resp);
                    break;
                case "bumenxlk":
                    bumensel(req,resp);
                    break;
                case "del":
                    this.del(req,resp);
                    break;
            }


        }else {
            int page = Integer.parseInt(req.getParameter("page"));
            int limit = Integer.parseInt(req.getParameter("limit"));
            System.out.println(page+","+limit);
            List<Map> maps = staffDaoimpl.selectALL(page,limit);
            int count =staffDaoimpl.selcount();
            Map map = new HashMap<>();
            map.put("code",0);
            map.put("msg","");
            map.put("count",count);
            map.put("data",maps);
            String s = JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
            PrintWriter writer = resp.getWriter();
            writer.print(s);
            writer.flush();
            writer.close();
        }


    }

    private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String gid = req.getParameter("gid");
        int delone =staffDaoimpl.delone(gid);
        resp.sendRedirect("yzruser/Staffshow.jsp");
    }

    private void bumensel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        List<Map> seljuese =staffDaoimpl.selbumen();
        String ss= JSON.toJSONString(seljuese);
        System.out.println(ss);
        writer.print(ss);
        writer.flush();
        writer.close();

    }


    //获取角色表里的所有sname
    private void sel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        List<Map> seljuese =staffDaoimpl.seljuese();
        System.out.println(seljuese);
        String t = JSON.toJSONString(seljuese);
        writer.print(t);
        writer.flush();
        writer.close();

    }
    //Staffshow.jsp中form表单的添加方法
    private void addstaff(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=UTF-8");
        Map<String, String[]> parameterMap = req.getParameterMap();


//        parameterMap.forEach((x,y)->{
//            System.out.println(x+" "+ Arrays.toString(y));
//        });


        Staff staff = new Staff();
        TransBean.populate(staff,parameterMap);
        System.out.println(staff);
        int i= staffDaoimpl.updone(staff);
        PrintWriter writer = resp.getWriter();
        writer.print(i);
        writer.flush();
        writer.close();

    }
}
